package beanstao.util.random;

/**
 */
public class StringGenerator
{
	/**
	 * @param max
	 *            必须大于0
	 */
	public StringGenerator(int max)
	{
		this.maxLen = max;
		this.minLen = 1;
	}

	/**
	 * 
	 * @param min
	 *            必须大于0
	 * @param max
	 *            必须不小于min
	 */
	public StringGenerator(int min, int max)
	{
		this.maxLen = max;
		this.minLen = min;
	}

	/**
	 * min length of the string
	 */
	private int maxLen;

	/**
	 * max length of the string
	 */
	private int minLen;

	/**
	 * 
	 * @param min
	 *            必须大于0
	 * @param max
	 *            必须不小于min
	 */
	public void setup(int max, int min)
	{
		this.maxLen = max;
		this.minLen = min;
	}

	/**
	 * 根据设置的max和min的长度,生成随机字符串.
	 * <p/>
	 * 若max或min小于0,则返回null
	 * 
	 * @return 生成的字符串
	 */
	public String next()
	{
		if ((this.maxLen <= 0) || (this.minLen <= 0))
		{
			return null;
		}
		char[] buf = new char[Randoms.random(this.minLen, this.maxLen)];
		for (int i = 0; i < buf.length; i++)
		{
			buf[i] = CharGenerator.next();
		}
		return new String(buf);
	}

}
